package skipClass.java;

import skipClass.java.ExamQuestion;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class QuestionAskActivity extends Activity
{
	private final static String TAG = "ClassSkippers";
	private Typeface font;
	private TextView HeaderTV;
	private TextView QuestionTV;
	private ListView AnswerLV;
	private Button CancelButton;
	
	private int QuestionPos;
	private String Question;
	private String RealAnswer;
	private ArrayList<String> BogusAnswers;
	private boolean CanExcuse;
	private ArrayList<String> AvailableExcuses;
	
	private ArrayList<String> Answers;
	private ArrayAdapter<String> AnswersAdapter;
	
	private static final int DEFAULT_LIST_LAYOUT = R.layout.question_ask_item;
	private static final int DEFAULT_LIST_TV = R.id.question_ask_item_tv;
	private static final int DEFAULT_EMPTY_LIST_LAYOUT = R.layout.question_ask_empty;
	
	private static final int CHOOSE_EXCUSE_REQUEST_CODE = 0;
	
	private class AnswerAdapter extends ArrayAdapter<String>
	{	
		ArrayList<String> Answers;
		int _textViewResourceId = -1;
		int _resource = -1;
		
		public AnswerAdapter(Context context, int textViewResourceId, String[] objects)
		{
			super(context, textViewResourceId, objects);
			
			_textViewResourceId = textViewResourceId;
			
			Answers = new ArrayList<String>(objects.length);
			
			for (int i = 0; i < objects.length; i++)
			{
				Answers.add(objects[i]);
			}
			
		}

		public AnswerAdapter(Context context, int resource, int textViewResourceId, String[] objects)
		{
			super(context, resource, textViewResourceId, objects);
			
			_resource = resource;
			_textViewResourceId = textViewResourceId;
			
			Answers = new ArrayList<String>(objects.length);
			
			for (int i = 0; i < objects.length; i++)
			{
				Answers.add(objects[i]);
			}
			
		}

		public AnswerAdapter(Context context, int resource, int textViewResourceId, List<String> objects)
		{
			super(context, resource, textViewResourceId, objects);
			
			_resource = resource;
			_textViewResourceId = textViewResourceId;
			
			Answers = new ArrayList<String>(objects);
		}

		public AnswerAdapter(Context context, int resource, int textViewResourceId)
		{
			super(context, resource, textViewResourceId);
			
			_resource = resource;
			_textViewResourceId = textViewResourceId;
			
			Answers = new ArrayList<String>();
		}

		public AnswerAdapter(Context context, int textViewResourceId, List<String> objects)
		{
			super(context, textViewResourceId, objects);
			
			_textViewResourceId = textViewResourceId;
			
			Answers = new ArrayList<String>(objects);
		}

		public AnswerAdapter(Context context, int textViewResourceId)
		{
			super(context, textViewResourceId);
			
			_textViewResourceId = textViewResourceId;
			
			Answers = new ArrayList<String>();
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			View v = convertView;
			
			if (v == null)
			{
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(QuestionAskActivity.this.DEFAULT_LIST_LAYOUT, null);
			}
			
			if (Answers.isEmpty())
			{
				View ev;
				
				LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                ev = vi.inflate(QuestionAskActivity.this.DEFAULT_EMPTY_LIST_LAYOUT, null);
                return ev;
			}
			
			String answer = Answers.get(position);
			
			TextView tv = (TextView) v.findViewById(QuestionAskActivity.this.DEFAULT_LIST_TV);
			tv.setTypeface(QuestionAskActivity.this.font);
			
			if (answer != null)
			{	
				tv.setText(answer); 
				
			}
			
			else
			{
				tv.setText("Invalid answer!");
			}
			
			return v;
		}
		
		@Override
		public int getCount()
		{
			return Answers.size();
		}
		
		@Override
		public void add(String object)
		{
			Answers.add(object);
		}
		
		public void addAll (ArrayList<String> collection)
		{
			Answers.addAll(collection);
		}
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.question_ask);
		
		font = Typeface.createFromAsset(getAssets(), "font/GoodDog.otf");
		
		HeaderTV = (TextView) findViewById(R.id.question_ask_tv);
		QuestionTV = (TextView) findViewById(R.id.question_ask_question_tv);
		AnswerLV = (ListView) findViewById(R.id.question_ask_lv);
		CancelButton = (Button) findViewById(R.id.question_ask_cancel);
		
		HeaderTV.setTypeface(font);
		QuestionTV.setTypeface(font);
		CancelButton.setTypeface(font);
		
		AnswerLV.setOnItemClickListener(
				new OnItemClickListener() 
				{
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) 
					{
						String answer = Answers.get(position);
						
						if (answer.equals(RealAnswer))
						{
							Intent intent = new Intent();
							intent.putExtra("QuestionPos", QuestionPos);
							intent.putExtra("AnswerStatus", ExamQuestion.CORRECT);
							setResult(RESULT_OK, intent);
							finish();
						}
						
						else
						{
							Intent intent = new Intent();
							intent.putExtra("QuestionPos", QuestionPos);
							intent.putExtra("AnswerStatus", ExamQuestion.WRONG);
							setResult(RESULT_OK, intent);
							finish();
						}
					}
				}
			);		
		
		
		Bundle extras = getIntent().getExtras();
		
		if (extras != null)
		{
			QuestionPos = extras.getInt("QuestionPos");
			Question = extras.getString("Question");
			RealAnswer = extras.getString("Answer");
			BogusAnswers = extras.getStringArrayList("BogusAnswers");
			CanExcuse = extras.getBoolean("CanExcuse");
			AvailableExcuses = extras.getStringArrayList("Excuses");
			
			QuestionTV.setText(Question);
			
			Answers = new ArrayList<String>();
			
			Answers.add(RealAnswer);
			
			for (int i = 0; i < BogusAnswers.size(); i++)
			{	
				Answers.add(BogusAnswers.get(i));
			}
			
			Collections.shuffle(Answers);
			
			AnswersAdapter = new ArrayAdapter<String>(this, R.layout.question_ask_item, R.id.question_ask_item_tv, Answers);
			
			AnswerLV.setAdapter(AnswersAdapter);
			
			Log.i(TAG, "Answers ListView adapter has " + AnswerLV.getAdapter().getCount() + " entries.");
			
			AnswersAdapter.notifyDataSetChanged();
			
		}
	}
	
	public boolean onCreateOptionsMenu(Menu menu)
	{
		if (CanExcuse)
		{
			MenuInflater inflator = getMenuInflater();
			inflator.inflate(R.menu.ask_question_option, menu);
		}
		
		return true;
		//new MenuInflater(this).inflate(R.menu.ask_question_option, menu);
		//return(super.onCreateOptionsMenu(menu));
	}
	
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch(item.getItemId())
		{	
			case R.id.excuse:
				Intent intent = new Intent(this, ChooseExcuseActivity.class);
			
				intent.putStringArrayListExtra("Excuses", AvailableExcuses);

				startActivityForResult(intent, CHOOSE_EXCUSE_REQUEST_CODE);
				break;
		}
		
		return true;
		//return(super.onOptionsItemSelected(item));
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent)
	{
		super.onActivityResult(requestCode, resultCode, intent);
		
		switch(requestCode)
		{
			case CHOOSE_EXCUSE_REQUEST_CODE:
			{
				switch(resultCode)
				{
					case RESULT_OK:
					{
						Bundle extras = intent.getExtras();
						
						Log.i(TAG, "Excusing question with excuse " + extras.getInt("ExcusePos"));
						
						Intent newIntent = new Intent();
						newIntent.putExtra("QuestionPos", QuestionPos);
						newIntent.putExtra("AnswerStatus", ExamQuestion.EXCUSED);
						newIntent.putExtra("ExcusePos", extras.getInt("ExcusePos"));
						setResult(RESULT_OK, newIntent);
						finish();
						break;
					}
					
					case RESULT_CANCELED:
					{
						break;
					}
				}
			}
		}
	}
	
	public void onClick(View v)
	{
		if (v == CancelButton)
		{
			Intent intent = new Intent();
			intent.putExtra("QuestionPos", QuestionPos);
			intent.putExtra("AnswerStatus", ExamQuestion.NOT_ATTEMPTED);
			setResult(RESULT_OK, intent);
			finish();
		}
	}
}
